package com.huluobo.a2206parkdemo

import android.content.Intent
import android.util.Log
import android.view.View
import android.webkit.JavascriptInterface
import com.blankj.utilcode.util.SPUtils
import com.huluobo.a2206parkdemo.databinding.ActivityMainBinding
import com.huluobo.a2206parkdemo.news.NewsActivity
import com.huluobo.a2206parkdemo.parking.ParkingActivity
import com.huluobo.a2206parkdemo.patrol.PatrolActivity
import com.huluobo.a2206parkdemo.repair.RepairActivity
import com.huluobo.a2206parkdemo.sign.SignActivity
import com.huluobo.a2206parkdemo.sign.SignMonthActivity
import com.huluobo.a2206parkdemo.visitor.VisitorActivity
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {
    private lateinit var listener: MainWebViewJavaScriptFun
    override fun initData() {
        vm.queryUserInfo(SPUtils.getInstance().getString(Const.PARAM_USERNAME))
        vm.queryUserInfoSuccess.observe(this) {
            Log.i("TagA", "userBean:$it")
        }
    }

    override fun initView() {
        binding.mainHeader.headerBack.visibility = View.GONE
        initWebView()
    }

    private fun initWebView() {
        listener = MainWebViewJavaScriptFun()
        //读取地址
        binding.mainX5.loadUrl(SPUtils.getInstance().getString(Const.PARAM_HOME))
        //设置网页的滚动条件,可以一直滚动
        binding.mainX5.view.overScrollMode = View.OVER_SCROLL_ALWAYS
        //设置与h5开发人员约定好的id
        binding.mainX5.addJavascriptInterface(listener, "androidinfo")
    }

    //设置和h5交互的id
    inner class MainWebViewJavaScriptFun {
        @JavascriptInterface
        fun androidsign(id: String) {
            start(SignActivity::class.java)
        }

        @JavascriptInterface
        fun androidaddculture(id: String) {
            //打开新闻activity
            start(NewsActivity::class.java)
        }

        @JavascriptInterface
        fun androidparking(id: String?) {
            //车位申请
            start(ParkingActivity::class.java)
        }

        @JavascriptInterface
        fun androidapply(id: String?) {
            //我的申请
            Log.i("TagA", "点击了我的申请")
        }

        @JavascriptInterface
        fun androidvisit(id: String?) {
            //来访预约
            start(VisitorActivity::class.java)
        }

        @JavascriptInterface
        fun androidpatrol(id: String?) {
            //巡更管理
            start(PatrolActivity::class.java)
        }

        @JavascriptInterface
        fun androidnews(id: String?) {
            Log.i("TagA", "点击了新闻")
        }

        @JavascriptInterface
        fun androidnoticelist(id: String?) {
            Log.i("TagA", "点击了公告")
        }

        @JavascriptInterface
        fun androidrepair(id: String?) {
            //维修管理
            start(RepairActivity::class.java)
        }

        @JavascriptInterface
        fun androidaddrepair(id: String?) {
            //添加维修
            Log.i("TagA", "点击了添加维修")
        }

        @JavascriptInterface
        fun androidaddnotice(id: String?) {
            //公告管理
            Log.i("TagA", "点击了添加公告")
        }

        @JavascriptInterface
        fun androidculture(id: String?) {
            //文化管理
            Log.i("TagA", "点击了文化管理")
        }

        @JavascriptInterface
        fun androidnotice(id: String?) {
            //公告管理
            Log.i("TagA", "点击了公告管理")
        }

        @JavascriptInterface
        fun androidattendance(id: String?) {
            //考勤管理
            start(SignMonthActivity::class.java)
        }

        @JavascriptInterface
        fun androidproperty(id: String?) {
            //物业审核
            Log.i("TagA", "点击了物业审核")
        }

        @JavascriptInterface
        fun androidcheckculture(id: String?) {
            //文化审核
            Log.i("TagA", "点击了文化审核")
        }

        @JavascriptInterface
        fun androidpeople(id: String?) {
            //人资管理
            Log.i("TagA", "点击了人资管理")
        }
    }

    private fun start(clazz: Class<*>) {
        val intent = Intent(this, clazz)
        startActivity(intent)
    }
}